<!-- 验证插件 -->
<script src="/static/plugins/jquery-validation/jquery.validate.min.js"></script>
<script src="/static/plugins/jquery-validation/localization/messages_zh.min.js"></script>
<!-- 针对当前主题进行简单封装，精简代码 -->
<script src="/static/sdtheme/scripts/sdvalidate.js"></script>
<script>
    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    $(function () {
        pageInit();
    });

    function pageInit() {
        //角色下拉框初始化
        roleSelectInit('{{.roles}}');

        //附值
        $("#IsSuper").selectpicker('val', '{{.m.IsSuper}}');
        $("#Status").selectpicker('val', '{{.m.Status}}');
        //验证
        validateInfo();
    }

    //角色下拉框初始化
    function roleSelectInit(selecteds) {
        var url = '{{urlfor "RoleController.DataList"}}';
        var $select = $("#Roles");
        $select.html('');

        $.sdpost(url, {}, function (re) {
            if (re.code === 0) {
                var html = [];
                $(re.obj).each(function (i, e) {
                    html.push('<option value="' + e.Id + '">' + e.Name + '</option>');
                });
                $select.html(html.join(''));
                $select.selectpicker({liveSearch: true});

                $select.selectpicker('val', selecteds.split(','));
            } else {
                layer.alert("获取数据失败", {icon: 2, title: '失败'});
            }
        });
    }

    //正则表达式验证
    jQuery.validator.methods.mobile = function (value, element, param) {
        //如果为空，直接过
        if (value.length == 0) return true;
        if (/^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\d{8}$/.test(value)) {
            return true;
        } else {
            return false;
        }
    }

    //表单验证及AJAX提交
    function validateInfo() {
        var $thisForm = $('#thisForm');
        $thisForm.sdvalidate({
            rules: {
                UserName: {
                    maxlength: 24,
                    required: true
                },
                RealName: {
                    maxlength: 16,
                    required: true
                },
                Mobile: {
                    mobile: true,
                    maxlength: 16,
                },
                Email: {
                    email: true,
                    maxlength: 256,
                },
            },
            messages: {
                Mobile: {
                    mobile: "请输入正确的手机号"
                }
            },
            submitHandler: function (form) {
                var url = '{{ urlfor "BackendUserController.Edit"}}';
                var params = $thisForm.serialize();
                $.sdpost(url, params, function (re) {
                    if (re.code === 0) {
                        parent.layer.msg(re.msg);
                        parent.refresh(re.obj);
                        parent.layer.close(index)
                    } else {
                        parent.layer.alert(re.msg, {icon: 2, title: '失败'})
                    }
                });
                return false;
            }
        });
    }

    function FormSubmit() {
        $("#thisForm").submit();
    }
</script>